package copyengine.resource.resInfo
{
    import copyengine.log.CELog;
    import copyengine.resource.CEResUtils;
    import copyengine.resource.resProxy.CEResFIleDesc;
    import copyengine.resource.resProxy.CEResProxy;
    import copyengine.resource.resProxy.loadQueue.CEResLoadQueue;

    import flash.utils.Dictionary;

    public class CEResInfoManger
    {
        private static var mInstance:CEResInfoManger;

        public static function get instance():CEResInfoManger {
            if (mInstance == null) {
                mInstance = new CEResInfoManger();
            }
            return mInstance;
        }

        private var mAllFileMetaDic:Dictionary;

        private var mResourceFilePath:String;
        private var mInitFinishCallback:Function;
        private var mInitResProxy:CEResProxy;
        private var mIsInitComplete:Boolean;
        private var mFilePathPR:String

        public function CEResInfoManger() {
        }

        public function initialize(_filePathPR:String, _resourceFilePath:String, _finishCallback:Function):void {
            mFilePathPR = _filePathPR;
            mResourceFilePath = _resourceFilePath;
            mInitFinishCallback = _finishCallback;

            mInitResProxy = new CEResProxy();
            var resQueue:CEResLoadQueue = new CEResLoadQueue(onConfigLoadComplete);

            var metaFileDesc:CEResFIleDesc = new CEResFIleDesc();
            metaFileDesc.fileName = mResourceFilePath;
            metaFileDesc.filePath = mFilePathPR + mResourceFilePath;
            metaFileDesc.fileType = CEResFIleDesc.FILE_TYPE_JSON;
            resQueue.pushNeedLoadRes(metaFileDesc);

            mInitResProxy.loadResQueue(resQueue);
        }

        private function onConfigLoadComplete():void {
            mAllFileMetaDic = new Dictionary();
            mIsInitComplete = true;

            var jsonObj:Object = CEResUtils.getJSONObj(mResourceFilePath, mInitResProxy);
            for each(var eachFileObj:Object in jsonObj["allFileInfoList"]) {
                var metaFile:MetaEachResFileInfo = new MetaEachResFileInfo();
                metaFile.fromJSON(eachFileObj, mFilePathPR);
                mAllFileMetaDic[metaFile.name] = metaFile;
            }
            mInitResProxy.dispose();
            CELog.log("CEResInfoManger init complete , resource.json load complete");

            mInitFinishCallback();
            mInitResProxy = null;
            mInitFinishCallback = null;
        }

        //==============================//
        //==Public Interface  Function
        //==============================//

        /**
         * 根据文件名 获得对应资源文件的相关加载信息.
         *
         * 其中包含
         * 1· 文件的加载路径 ex: res/img/avatar/01.png
         * 2· 文件的大小(字节数)
         */
        public function getFileInfoByFileName(_fileName:String):MetaEachResFileInfo {
            //======================
            //= 检测是否Init,没Init则直接return
            //======================
            if (!mIsInitComplete) {
                CELog.err("CEResInfoManger not been init, call initialize function first ");
                return null;
            }
            return mAllFileMetaDic[_fileName];
        }


    }
}
